package yxy.game.pm2.command.tower;

import yxy.cherry.battle.module.Position;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.bean.task.TaskType;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.TowerMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.FighterModule;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.task.TaskHandle;
import yxy.game.pm2.module.tower.TowerHandle;
import yxy.game.pm2.module.tower.TowerModule;

import java.util.ArrayList;
import java.util.Map;

/**
 * 修炼塔 修炼塔玩家战力远超塔战力跳过打斗
 */
@MessageType(proto = Command.TowerPowerOverSweep, classtype = TowerMessage.TowerPowerOverSweepRequest.class)
public class CMD_TowerPowerOverSweep extends Command<TowerMessage.TowerPowerOverSweepRequest> {

    @Override
    public void execute(MessageSource messageSource, TowerMessage.TowerPowerOverSweepRequest request) {
        String playerCode = messageSource.getTag();//玩家id
        int floor = request.getStage();//关卡
        TowerMessage.TowerPowerOverSweepResponse.Builder response = TowerMessage.TowerPowerOverSweepResponse.newBuilder();
        TowerModule towerModule = TowerModule.get(playerCode);//塔缓存
        TowerHandle towerHandle = new TowerHandle();//塔处理器

        try {
            // 这层还没有挑战成功，不能进行挑战跳过
            if (floor > towerModule.getFloor() + 1) {
                send(messageSource, response, Result.TowerChanllengeFloor);
                return;
            }

            //这层是之前的关卡，不能重复进行挑战
            if (floor < towerModule.getFloor()) {
                send(messageSource, response, Result.TowerBonusRewarded);
                return;
            }

            //校验玩家的战力*0.7要大于怪的战力才能进入此接口
            if (!playerTotalPowerIsOverTower(floor, playerCode, towerHandle)) {
                send(messageSource, response, Result.TowerPowerOverPlayerPower);
                return;
            }

            ArrayList<Integer> maxFloor = getMaxFloor(floor, playerCode, towerHandle);

            //下一关,并发控制奖品不能领多份
            Boolean next = towerHandle.nextFloor(playerCode, maxFloor.get(maxFloor.size()-1));
            if (!next) {
                send(messageSource, response, Result.TowerBonusRewarded);
                return;
            }

            //返回初次挑战成功的奖励
            ArrayList<Currency> rewards = new ArrayList<Currency>();
            for (int i = 0; i <maxFloor.size(); i++) {
                rewards.addAll(TowerHandle.floorRewards(maxFloor.get(i), true));
            }
            rewards = new ArrayList<>(Currency.merge(rewards));

            new CurrencyHandle().addCurrency(playerCode, CCReason.修炼塔, rewards);
            response.addAllRewards(new CurrencyHandle().packItems(rewards));

            TaskHandle.handle(playerCode, TaskType.指标型_修练塔_关卡_达到, towerModule.getFloor());
            send(messageSource, response, Result.Success);

        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }


    private void send(MessageSource messageSource, TowerMessage.TowerPowerOverSweepResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


    //判断玩家战力是否远大于怪的战力
    private boolean playerTotalPowerIsOverTower(int floor, String playerCode, TowerHandle towerHandle) {
        Map<Position, Hero> map = new FormatHandle().formatHeros(playerCode, Format.FormatType.普通);
        //玩家阵容总战力
        long playerTotalPower = 0;
        for (Map.Entry<Position, Hero> entry : map.entrySet()) {
            Hero hero = entry.getValue();
            playerTotalPower += hero.getPower();
        }
        //塔的怪总战力
        ArrayList<FighterModule> fighterModuleList = towerHandle.getFighterModuleList(floor);
        long towerTotalPower = 0;
        for (FighterModule fighterModule : fighterModuleList) {
            long power = fighterModule.getTotalPower();
            towerTotalPower += power;
        }
        return playerTotalPower * 0.7 >= towerTotalPower;
    }


    //获取极限的关卡 ： 个人战力大于怪的战力
    private ArrayList<Integer> getMaxFloor(int floor, String playerCode, TowerHandle towerHandle) {
        ArrayList<Integer> floors = new ArrayList<>();
        Map<Position, Hero> map = new FormatHandle().formatHeros(playerCode, Format.FormatType.普通);
        //玩家阵容总战力
        long playerTotalPower = 0;
        for (Map.Entry<Position, Hero> entry : map.entrySet()) {
            Hero hero = entry.getValue();
            playerTotalPower += hero.getPower();
        }
        //塔的怪总战力
        long towerTotalPower = 0;
        while (playerTotalPower * 0.7 >= towerTotalPower) {
            towerTotalPower = 0;
            ArrayList<FighterModule> fighterModuleList = towerHandle.getFighterModuleList(floor);
            for (FighterModule fighterModule : fighterModuleList) {
                long power = fighterModule.getTotalPower();
                towerTotalPower += power;
            }
            floors.add(floor);
            floor++;
        }

        return floors;
    }
}